package com.glorysoft.sunnypda.ui.autograph;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.glorysoft.sunnypda.R;
import com.glorysoft.sunnypda.base.Constant;
import com.glorysoft.sunnypda.cutstomview.LinePathView;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class AutographActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_autograph);

        LinePathView lView = findViewById(R.id.lView);
        TextView tvDelete = findViewById(R.id.tvDelete);
        TextView tvConfirm = findViewById(R.id.tvConfirm);
        ImageView ivBack = findViewById(R.id.ivBack);
        TextView tvCurrentUser = findViewById(R.id.tvCurrentUser);

        tvCurrentUser.setText(Constant.Companion.getCURRENT_USER_NAME());
        ivBack.setOnClickListener(v -> finish());

        lView.setBackColor(Color.WHITE);
        lView.setPaintWidth(20);
        lView.setPenColor(Color.BLACK);

        tvDelete.setOnClickListener(v -> lView.clear());
        tvConfirm.setOnClickListener(v -> {
            if (lView.getTouched()) {
                try {
                    String path = "/sdcard/" + FreeApi_DateUtils.getNowStringDate("yyyyMMddHHmmss") + ".png";
                    lView.save(path, true, 10);
                    Intent intent = getIntent();
                    intent.putExtra("BITMAP",fileToBase64(new File(path)));
                    setResult(RESULT_OK,intent);
                    finish();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else {
                FreeApi_ToastUtils.showFreeToast("您还没有签名",AutographActivity.this,false, Toast.LENGTH_SHORT);
            }
        });
    }

    /**
     * file 转base64
     * @return
     */
    public String fileToBase64(File file) {
        String base64 = null;
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            byte[] bytes = new byte[in.available()];
            int length = in.read(bytes);
            base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return base64;

    }
}